package com.cn.aam.checaiduo.ui.homepage.pay;

import android.content.Context;
import android.content.Intent;
import android.webkit.WebView;

import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.web.BaseWebActiivity;
import com.cn.aam.checaiduo.bean.me.RedirectContentB;
import com.cn.aam.checaiduo.center.CustomDialogCenter;

/**
 * @author 艹羊
 * @project Checaiduo_Android
 * @date 2017/6/20 下午2:39
 * @description
 */

public class PayActivity extends BaseWebActiivity {
    public static final String DATA_ORDER_ID = "data_order_id";//订单ID


    private String orderId;

    public static Intent newInstance(Context context, String targetUrl, String targetTitle, String orderId) {
        Intent i = new Intent(context, PayActivity.class);
        i.putExtra(Const.TARGET_URL, targetUrl);
        i.putExtra(Const.TARGET_TITLE, targetTitle);
        i.putExtra(DATA_ORDER_ID, orderId);
        return i;
    }


    @Override
    protected void onGenerate() {
        super.onGenerate();
        orderId = getIntent().getStringExtra(DATA_ORDER_ID);

    }

    @Override
    protected void initView() {
        super.initView();
    }


    @Override
    protected void OverrideUrlLoading(WebView view, String url) {
        if (url.contains("Usercenter/finance"))
            rechargeSuccess();
        if (url.contains("InternalApi/paySuccess"))
            paySuccess();
    }

    /**
     * 充值成功
     */
    private void rechargeSuccess() {
        //余额不足的情况下，1.提交订单 2.充值 3.再次发起支付请求
        RetrofitHome.getInstance()
                .getInternalApi()
                .pay(AppData.getToken(), orderId)
                .compose(RxSchedulers.<RedirectContentB>toMain())
                .subscribe(new OnRequestListener<RedirectContentB>(this) {
                    @Override
                    protected String getProgressBarContent() {
                        return "付款中...";
                    }

                    @Override
                    public void onNext(RedirectContentB value) {
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_FAILED) {
                            return;
                        }

                        loadEverThing(value.getRedirect_content());

                    }
                });
    }

    /**
     * 付款成功
     */
    private void paySuccess() {
        startActivity(PayResultSuccessActivity.newInstance(mContext, orderId));
        setResult(RESULT_OK);
        finish();

    }


    private void showDialog() {
        CustomDialogCenter
                .getInstance()
                .showTipsDialog(mContext, "抱歉！支付失败", "退出支付", "重新支付", new CustomDialogCenter.OnTipClick() {
                    @Override
                    public void onLeftButtonClick() {
                        finish();
                    }

                    @Override
                    public void onRightButtonClick() {

                    }
                });
    }


}
